package org.dolphin.base.jsontools;

import org.dolphin.base.data.vo.BaseVO;
import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.ArrayList;

import net.sf.json.*;

public class JsonConverter {
	
	public static JSONObject getJSONObjectFromObject(Object o) {		
		return JSONObject.fromObject(o);
	}
	
	public static JSONObject getJSONObjectFromObject(String fields,Object o) {
		JSONObject jo = new JSONObject();
		Field[] fd = o.getClass().getDeclaredFields();
		for (int i =0;i < fd.length;i++) {
			if (fields.indexOf(fd[i].getName().trim()) > -1) {
				jo.put(fd[i].getName(), BaseVO.getObject(o.getClass(),o, fd[i].getName()));
			}
		}
		return jo;
	}

	public static Object getObjectFromJSON(JSONObject jobj,Class cl) {
		return JSONObject.toBean(jobj, cl);
	}	
	
	public static Object getObjectFromJSON(String jobj,Class cl) {
		JSONObject obj = JSONObject.fromObject(jobj);
		return JSONObject.toBean(obj, cl);
	}
	
	public static String[] getJsonFieldArray(JSONObject jsonobj) {
		String fs = "";
    	for (Iterator its = jsonobj.keys();its.hasNext();) {
    		String s = (String)its.next();
    		fs = fs + s + ",";
    		
    	}
		if (fs != "") {
			if (fs.charAt(fs.length()-1) == ',') {
				fs = fs.substring(0,fs.length()-1);
			}
			return BaseVO.getFieldArray(fs, ",");
		}
		return null;
	}

	public static String getJsonFieldString(JSONObject jsonobj) {
		String fs = "";
    	for (Iterator its = jsonobj.keys();its.hasNext();) {
    		String s = (String)its.next();
    		fs = fs + s + ",";
    		
    	}
		if (fs != "") {
			if (fs.charAt(fs.length()-1) == ',') {
				fs = fs.substring(0,fs.length()-1);
			}
			return fs;
		}
		return "";
	}	
	
	public static boolean isNumber(String name) {
		String[] fs = {"Integer","Float","Double","Long","Short"};
		for (int i = 0;i<fs.length;i++) {
			if (name.equalsIgnoreCase(fs[i])) {
				return true;
			}
		}
		return false;
	}
	
	public static ArrayList Object2ArrayList(String fields,ArrayList al) {
		ArrayList toal = new ArrayList();
		if (al != null) {
			for (int j = 0;j < al.size();j++) {
				String res = JsonConverter.Object2RowString(fields,al.get(j));
				res = res.replace("\r\n","");
				toal.add(res);
			}
		}
		return toal;		
	}
	
	public static String StringList2String(ArrayList al) {
		String returns="";
		for (int i =0;i < al.size();i++) {
			String s = (String)al.get(i);
			returns = returns+s;
			if (i < al.size()-1) {
				returns = returns+",";
			}
		}
		return "["+returns+"]";

	}
	
	public static String Object2RowString(String fields,ArrayList al) {
		String outs = "";
		if (al != null) {
			for (int j = 0;j < al.size();j++) {
				String res = JsonConverter.Object2RowString(fields,al.get(j));				
				res = res.replace("\r\n","");
				if (j < al.size()-1) {
					outs = outs + res + ",";
				} else {
					outs = outs+res;
				}
			}
			outs = "["+outs+"]";
		}
		return outs;		
	}
	
	public static String Object2RowString(String [] Fields,ArrayList al) {
		String outs = "";
		//System.out.println(outs);
		if (al != null) {
			for (int j = 0;j < al.size();j++) {
				String res = JsonConverter.Object2RowString(Fields,al.get(j));
				res = res.replace("\r\n","");
				if (j < al.size()-1) {
					outs = outs + res + ",";
				} else {
					outs = outs+res;
				}
			}
			outs = "["+outs+"]";
		}
		return outs;
	}
	
	public static String Object2RowString(Class cl,ArrayList al) {
		Field[] fs = cl.getDeclaredFields();
		String fields = "";
		for (int i = 0;i < fs.length;i++) {
			fields = fields + fs[i].getName();
			if (i < fs.length-1) {
				fields = fields + ",";
			}			
		}
		//System.out.println(fields);
		return JsonConverter.Object2RowString(fields.split(","),al);
	}
	

	public static String Object2RowString(String[] fields,Object obj) {
		String outstring = "";
		for (int i = 0;i < fields.length;i++) {
			String s = "";
			//System.out.println(fields[i]);
			Object a = BaseVO.getObject(obj.getClass(), obj, fields[i]);
			if (JsonConverter.isNumber(a.getClass().getSimpleName())) {
				s = a.toString();
			} else {
				s = "'"+a.toString()+"'";
			}
			if (i < fields.length-1) {
				outstring = outstring + s + ",";
			} else {
				outstring = outstring + s;
			}			
		}
		if (outstring.equalsIgnoreCase("")) {
			return outstring;
		} else {
			outstring = "["+outstring+"]";
		}
		return outstring.replace("\r\n"," ");
	}	
	
	public static String Object2RowString(Object obj) {
		Field[] fs = obj.getClass().getDeclaredFields();
		String outstring = "";
		for (int i = 0;i < fs.length;i++) {
			String s = "";
			Object a = BaseVO.getObject(obj.getClass(), obj, fs[i].getName());
			if (JsonConverter.isNumber(a.getClass().getSimpleName())) {
				s = a.toString();
			} else {
				s = "'"+a.toString()+"'";
			}
			if (i < fs.length-1) {
				outstring = outstring + s + ",";
			} else {
				outstring = outstring + s;
			}			
		}
		if (outstring.equalsIgnoreCase("")) {
			return outstring;
		} else {
			outstring = "["+outstring+"]";
		}
		return outstring.replace("\r\n"," ");
	}	
	
	
	public static String Object2RowString(String fields,Object obj) {
		String[] fs = BaseVO.getFieldArray(fields,",");
		String outstring = "";
		for (int i = 0;i < fs.length;i++) {
			String s = "";
			Object a = BaseVO.getObject(obj.getClass(), obj, fs[i]);
			if (JsonConverter.isNumber(a.getClass().getSimpleName())) {
				s = a.toString();
			} else {
				s = "'"+a.toString()+"'";
			}
			if (i < fs.length-1) {
				outstring = outstring + s + ",";
			} else {
				outstring = outstring + s;
			}			
		}
		if (outstring.equalsIgnoreCase("")) {
			return outstring;
		} else {
			outstring.replace("\r\n"," ");
			return "["+outstring+"]";
		}
	}
	
	public static String Object2Item(String idfield,String[] captionfield,String pt,Object o) {
		if (captionfield == null) {
			return null;
		}
		String outstring = "";
		String s = pt;
		Object a = BaseVO.getObject(o.getClass(), o, idfield);
		JSONObject jo = new JSONObject();
		jo.put("id", a);
		for (int i = 0;i < captionfield.length;i++) {
			Object v = BaseVO.getObject(o.getClass(),o,captionfield[i]);
			s = s.replaceAll(captionfield[i], String.valueOf(v));			
		}
		jo.put("caption", s);
		jo.toString();
		return jo.toString().replace("\r\n"," ");
	}
	
	public static String Object2Item(String idfield,String captionfield,Object o) {
		if (captionfield == null || captionfield == "") {
			captionfield = idfield;
		}
		String outstring = "";
		String s = "";
		Object a = BaseVO.getObject(o.getClass(), o, idfield);
		JSONObject jo = new JSONObject();
		jo.put("id", a);
		Object b = BaseVO.getObject(o.getClass(),o,captionfield);
		jo.put("caption", b);
		jo.toString();
		return jo.toString().replace("\r\n"," ");
	}	

	public static String Object2Items(String idfield,String[] captionfield,String pt,ArrayList al) {
		String res = "";
		for (int i = 0;i < al.size();i++) {
			String item = Object2Item(idfield,captionfield,pt,al.get(i));
			if (i != al.size()-1) {
				res = res + item + ",";
			} else {
				res = res + item;
			}
		}
		res = "["+res+"]";
		return res;
	}
	
	public static String Object2Items(String idfield,String captionfield,ArrayList al) {
		String res = "";
		for (int i = 0;i < al.size();i++) {
			String item = Object2Item(idfield,captionfield,al.get(i));
			if (i != al.size()-1) {
				res = res + item + ",";
			} else {
				res = res + item;
			}
		}
		res = "["+res+"]";
		return res;
	}
	
	public static String[] JSON2StringArray(String s) {
		s = s.replace('[',' ');
		s = s.replace(']',' ');
		s = s.trim();
		String [] a = s.split("},");
		for (int i = 0;i < a.length;i++) {
			if (a[i].lastIndexOf("}") <0) {
				a[i] = a[i]+"}";
			}			
		}		
		return a;
	}
	
	public static ArrayList JSON2ArrayList(String s,Class c) {
		String[] sa = JSON2StringArray(s);
		ArrayList al = new ArrayList();
		for (int i =0;i < sa.length;i++) {
			Object o = getObjectFromJSON(sa[i],c);
			al.add(o);
		}
		if (al.size() > 0) {
			return al;
		}
		return null;
	}

}
